import 'package:core/route/core_navigator_observer.dart';
import 'package:flutter/material.dart';

class NavigatorHelper {
  const NavigatorHelper._();

  ///跳转新页面
  static pushNamed(BuildContext context, String routeName, {Map? arguments, bool? removeAll, bool? replacement, bool? repeat}) {
    //routeName移到桟顶
    if (removeAll == true) {
      Navigator.of(context).pushNamedAndRemoveUntil(routeName, (route) => false);
      return;
    }
    //routeName替换当前页面
    if (replacement == true) {
      Navigator.of(context).pushReplacementNamed(routeName, arguments: arguments);
      return;
    }
    //允许页面重复
    if (repeat == true) {
      Navigator.of(context).pushNamed(routeName, arguments: arguments);
      return;
    }
    //堆栈中包含此页面,则直接回退到该页面
    if (CoreNavigatorObserver.history.contains(routeName)) {
      Navigator.of(context).popUntil(ModalRoute.withName(routeName));
      return;
    }
    //直接push到该路由
    Navigator.of(context).pushNamed(routeName, arguments: arguments);
  }

  ///退出页面
  static pop<T extends Object?>(BuildContext context, {T? arguments}) {
    if (Navigator.canPop(context)) Navigator.of(context).pop(arguments);
  }
}
